﻿// Type: trx2html.Parser.TrxParser
// Assembly: RidoTasks, Version=0.7.3.1523, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\Users\jigar\Desktop\Vinay\trx2html_0.7\0.7.3\RidoTasks.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace trx2html.Parser
{
    internal class TrxParser
    {
        private XNamespace ns = (XNamespace)"http://microsoft.com/schemas/VisualStudio/TeamTest/2010";

        private ErrorInfo ParseErrorInfo(XElement r)
        {
            ErrorInfo errorInfo = new ErrorInfo();
            if (r.Element(this.ns + "Output") != null && r.Element(this.ns + "Output").Element(this.ns + "ErrorInfo") != null && r.Element(this.ns + "Output").Element(this.ns + "ErrorInfo").Element(this.ns + "Message") != null)
                errorInfo.Message = r.Element(this.ns + "Output").Element(this.ns + "ErrorInfo").Element(this.ns + "Message").Value;
            if (Enumerable.Count<XElement>(r.Descendants(this.ns + "StackTrace")) > 0)
                errorInfo.StackTrace = Enumerable.FirstOrDefault<XElement>(r.Descendants(this.ns + "StackTrace")).Value;
            if (Enumerable.Count<XElement>(r.Descendants(this.ns + "DebugTrace")) > 0)
                errorInfo.StdOut = Enumerable.FirstOrDefault<XElement>(r.Descendants(this.ns + "DebugTrace")).Value.Replace("\r\n", "<br />");
            return errorInfo;
        }

        private string GetSafeValue(XElement el, XName name)
        {
            string str = string.Empty;
            if (el.Element(name) != null)
                str = el.Element(name).Value;
            return str;
        }

        private string GetSafeAttrValue(XElement el, XName name, XName atribName)
        {
            string str = string.Empty;
            if (el.Element(name) != null && el.Element(name).Attribute(atribName) != null)
                str = el.Element(name).Attribute(atribName).Value;
            return str;
        }

        private TimeSpan ParseDuration(XElement el, string attName)
        {
            TimeSpan timeSpan = new TimeSpan(0L);
            if (el.Attribute((XName)attName) != null)
                timeSpan = TimeSpan.Parse(el.Attribute((XName)attName).Value);
            return timeSpan;
        }

        public TestRunResult Parse(string filePath)
        {
            XDocument xdocument = XDocument.Load(filePath);
            XNamespace ns = (XNamespace)"http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
            return new TestRunResult("", "", null);
            //  return new TestRunResult(xdocument.Document.Root.Attribute((XName) "name").Value, xdocument.Document.Root.Attribute((XName) "runUser").Value, Enumerable.Select(Enumerable.OrderBy(Enumerable.Join(Enumerable.Select(Enumerable.Select(Enumerable.Select((IEnumerable<XElement>) Enumerable.ToList<XElement>(xdocument.Descendants(ns + "UnitTest")), u => new 

            //  {
            //    u = u,
            //    id = u.Element(ns + "Execution").Attribute((XName) "id").Value
            //  }), param0 => new
            //  {
            //    \u003C\u003Eh__TransparentIdentifier0 = param0,
            //    desc = this.GetSafeValue(param0.u, ns + "Description")
            //  }), param0 => new
            //  {
            //    \u003C\u003Eh__TransparentIdentifier1 = param0,
            //    testClass = this.GetSafeAttrValue(param0.\u003C\u003Eh__TransparentIdentifier0.u, ns + "TestMethod", (XName) "className")
            //  }), (IEnumerable<XElement>) Enumerable.ToList<XElement>(xdocument.Descendants(ns + "UnitTestResult")), param0 => param0.\u003C\u003Eh__TransparentIdentifier1.\u003C\u003Eh__TransparentIdentifier0.id, (Func<XElement, string>) (r => r.Attribute((XName) "executionId").Value), (param0, r) => new
            //  {
            //    \u003C\u003Eh__TransparentIdentifier2 = param0,
            //    r = r
            //  }), param0 => param0.\u003C\u003Eh__TransparentIdentifier2.testClass), param0 => new TestMethodRun()
            //  {
            //    TestClass = param0.\u003C\u003Eh__TransparentIdentifier2.testClass,
            //    TestMethodName = param0.\u003C\u003Eh__TransparentIdentifier2.\u003C\u003Eh__TransparentIdentifier1.\u003C\u003Eh__TransparentIdentifier0.u.Attribute((XName) "name").Value,
            //    Description = param0.\u003C\u003Eh__TransparentIdentifier2.\u003C\u003Eh__TransparentIdentifier1.desc,
            //    Status = param0.r.Attribute((XName) "outcome").Value,
            //    Duration = this.ParseDuration(param0.r, "duration"),
            //    ErrorInfo = this.ParseErrorInfo(param0.r),
            //    ComputerName = param0.r.Attribute((XName) "computerName").Value
            //  }));
        }
    }
}
